package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Business> listBusiness(BusinessDto business) {
        Page<Business> list=businessMapper.listBusiness(new Page<Business>(business.getPage(),business.getPageSize()),business);
        return new PageResult<>(list.getTotal(),list.getRecords());

    }

    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }

    @Transactional(rollbackFor = Exception.class)//事务控制-默认只有运行时异常和错误才会回滚。
    @Override
    public void trackBusiness(Business business) {
        //1\基本属性的更新
        business.setStatus(3);//跟进中
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //2\添加跟踪记录表
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());//商机ID
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//跟进人ID-当前登录用户ID
        businessTrackRecord.setNextTime(business.getNextTime());//下次跟进时间
        businessTrackRecord.setTrackStatus(business.getTrackStatus());//跟进状态
        businessTrackRecord.setRecord(business.getRecord());//跟进记录
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());//沟通重点
        businessTrackRecord.setCreateTime(LocalDateTime.now());//创建时间
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Transactional(rollbackFor = Exception.class)//事务控制-默认只有运行时异常和错误才会回滚。
    @Override
    public void toCustomer(Integer id) {
        //1.根据商机ID查询上信息
        Business business = businessMapper.selectById(id);
        //2.更新状态
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //3.封装商品信息,组装客户信息,保存客户信息
        Customer customer = new Customer();
        BeanUtils.copyProperties(business,customer);
        customer.setId(null);//客户ID
        customer.setBusinessId(id);//商机ID
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());

        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> getBusinessPools(BusinessDto businessDto) {
        Page<Business> page=businessMapper.getBusinessPools(new Page<>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
}
